﻿using System;
using Company.BLL;
using Company.BLL.KDT.GC;
using Company.BLL.Utils;
using Company.BLL.DuLieuChuan;
using System.Data;
using Janus.Windows.GridEX;
using System.Windows.Forms;

namespace Company.Interface.KDT.GC
{
    public partial class DinhMucSelectSanPhamForm : BaseForm
    {        
        public SanPham SPDetail;
        private SanPhamCollection spCollection = new SanPhamCollection();
        public HopDong hongdong = new HopDong();
        public bool isAdd = true;
        public bool isUpdate = false;
        private int STTHang = 0;
        public bool isBrower = false;
        public DinhMucSelectSanPhamForm()
        {
            InitializeComponent();           
            SPDetail = new SanPham();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.SPDetail = null;
            this.Close();
        }
       
        
        private void LoadDanhSachChuaSanPham()
        {
            PhuKienDangKyCollection pkdkCollection = PhuKienDangKy.SelectCollectionBy_HopDong_IDAndMaPhuKienDaDuyet(hongdong.ID, "S13");
            PhuKienDangKy pkHopDong = new PhuKienDangKy();
            pkHopDong.SoTiepNhan = 0;
            pkHopDong.MaPhuKien = "Hợp đồng";
            pkdkCollection.Add(pkHopDong);
            cbHopDong.DataSource = pkdkCollection;
            cbHopDong.DisplayMember = "MaPhuKien";
            cbHopDong.ValueMember = "ID";
            cbHopDong.SelectedIndex = pkdkCollection.Count - 1;
        }
        private void SanPhamGCEditForm_Load(object sender, EventArgs e)
        {
                LoadDanhSachChuaSanPham();
                dgList.AllowDelete = Janus.Windows.GridEX.InheritableBoolean.False;
                spCollection = SanPham.SelectCollectionBy_HopDong_ID(hongdong.ID);
                dgList.DataSource = spCollection;
              
        }
   
  
        private void dgList_DeletingRecord(object sender, Janus.Windows.GridEX.RowActionCancelEventArgs e)
        {
            if (hongdong.TrangThaiXuLy == TrangThaiXuLy.DA_DUYET)
            {
                ShowMessage("Hợp đồng đã duyệt chính thức. Không được chỉnh sửa",false);
                e.Cancel = true;
                return;
            }
            if (e.Row.RowType == RowType.Record)
            {
                if (ShowMessage("Bạn có muốn xóa sản phẩm này không?", true) == "Yes")
                {
                    SanPham sp1 = new SanPham();
                    sp1.ID = (long)Convert.ToInt64(e.Row.Cells[2].Text);
                    if (sp1.ID > 0)
                    {
                        sp1.Delete();
                        isUpdate = true;
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        private void dgList_RowDoubleClick(object sender, Janus.Windows.GridEX.RowActionEventArgs e)
        {
            SPDetail.STTHang = (int)Convert.ToDecimal(e.Row.Cells[1].Text);
            SPDetail.SoLuongDangKy = Convert.ToDecimal(e.Row.Cells[8].Text);
            SPDetail.MaHS = e.Row.Cells[5].Text.Trim();
            SPDetail.Ten = e.Row.Cells[4].Text.Trim();
            SPDetail.DVT_ID = e.Row.Cells[6].Text;
            SPDetail.Ma = e.Row.Cells[3].Text.Trim();
            SPDetail.HopDong_ID = hongdong.ID;          
            this.Close();            
        }

        private void dgList_LoadingRow(object sender, RowLoadEventArgs e)
        {
            e.Row.Cells[7].Text = this.DonViTinh_GetName(e.Row.Cells[6].Value).Trim();
        }

        private void cbHopDong_ValueChanged(object sender, EventArgs e)
        {
            long id = Convert.ToInt64(cbHopDong.Value);
            if (id == 0)
            {
                spCollection = new SanPhamCollection();
                spCollection = SanPham.SelectCollectionBy_HopDong_ID(hongdong.ID);                
                dgList.DataSource = spCollection;
            }
            else
            {
                PhuKienDangKy pkDuyet = (PhuKienDangKy)cbHopDong.DropDownList.GetRow().DataRow;
                spCollection = new SanPhamCollection();
                PhuKienCollection collection = PhuKien.SelectCollectionTrangThaiDuyetById_HopDong(hongdong.ID, "S13", pkDuyet.SoPhuKien);
                foreach (PhuKien pk in collection)
                {
                    SanPham sp = new SanPham();
                    sp.Ma = pk.MaHang;
                    sp.MaHS = pk.MaHS;
                    sp.SoLuongDangKy = pk.SoLuong;
                    sp.STTHang = pk.STTHang;
                    sp.Ten = pk.TenHang;
                    sp.DVT_ID = pk.DVT_ID;
                    spCollection.Add(sp);
                }
                dgList.DataSource = spCollection;
            }           
        }
    }
}